package com.nsn.vs.sdm.util.shell;

import com.nsn.vs.sdm.util.string.UtilString;

import net.neoremind.sshxcute.core.ConnBean;
import net.neoremind.sshxcute.core.Result;
import net.neoremind.sshxcute.core.SSHExec;
import net.neoremind.sshxcute.exception.TaskExecFailException;
import net.neoremind.sshxcute.task.CustomTask;
import net.neoremind.sshxcute.task.impl.ExecCommand;
import net.neoremind.sshxcute.task.impl.ExecShellScript;

/**
 * Helper class for shell operation
 * 
 * @author Jeccy.Zhao
 *
 */
public class UtilShell
{
	/**
	 * Instance of SSH executor
	 */
	private SSHExec ssh = null;
	
	/**
	 * Flag that indicates whether connection built up or not
	 */
	private boolean isConnected = false;
	
	/**
	 * 
	 * @param host
	 * @param usr
	 * @param pwd
	 */
	public UtilShell(String host, String usr, String pwd)
	{
		ConnBean cb = new ConnBean(host, usr, pwd);
		ssh = SSHExec.getInstance(cb);
	}
	
	/**
	 * Connects to the specified host
	 * @return
	 */
	public void connect()
	{
		isConnected = ssh.connect();
	}
	
	/**
	 * Uploads single data to server
	 * 
	 * @param file
	 * @param path
	 * @throws Exception
	 */
	public void uploadSingleData(String file, String path) throws Exception
	{
		ssh.uploadSingleDataToServer(file, path);
	}
	
	/**
	 * Uploads all the files laid in the directory to server
	 * 
	 * @param directory
	 * @param path
	 * @throws Exception 
	 */
	public void uploadAllData (String directory, String path) throws Exception
	{
		ssh.uploadAllDataToServer(directory, path);
	}
	
	/**
	 * Executes the specified command and return output message
	 * 
	 * @param command
	 * @return
	 * @throws TaskExecFailException 
	 */
	public String executeCommand (String command) throws TaskExecFailException
	{
		return execute(new ExecCommand(command));
	}
	
	/**
	 * Executes the specified shell script
	 * 
	 * @param path
	 * @param script
	 * @param args
	 * @return
	 * @throws TaskExecFailException 
	 */
	public String executeScript (String path, 
			String script, String... args) throws TaskExecFailException
	{
		String argInput = "";
		if (args != null && args.length > 0)
		{
			argInput = UtilString.join(args, " ");
		}
		
		return execute(new ExecShellScript(path,"./" + script, argInput));
	}
	
	/**
	 * Execute task and return output message
	 * 
	 * @param ct
	 * @return
	 * @throws TaskExecFailException 
	 */
	private String execute (CustomTask ct) throws TaskExecFailException
	{
		Result rs = ssh.exec(ct);
		
		if (rs.isSuccess || !UtilString.isNotEmpty(rs.error_msg))
		{
			return rs.sysout;
		}
		else
		{
			throw new RuntimeException(rs.error_msg);
		}
	}
	
	/**
	 * Disconnects with the specified host
	 * @return
	 */
	public void disConnect()
	{
		if (isConnected)
		{
			ssh.disconnect();
			isConnected = false;
		}
	}
	
	public void setSsh(SSHExec ssh)
	{
		this.ssh = ssh;
	}

	public SSHExec getSsh()
	{
		return ssh;
	}

	public boolean isConnected()
	{
		return isConnected;
	}
	
}
